let data = {};
let editingId = null;
let requestUrl = null;

function addHandler() {
	let fabaoId = $("#fabao_id").combobox("getValue");

	if (fabaoId) {
		$("#fabaoId").combobox("setValue", fabaoId);
	}

	requestUrl = "/fabao_sixiang_attribute_category/insert";

	$("#sixiang_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#sixiang_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/fabao_sixiang_attribute_category/updateById";

		$("#id").val(rowData.id);
		$("#type").combobox("setValue", rowData.type);
		$("#effect").textbox("setValue", rowData.effect);
		$("#fabaoId").combobox("setValue", rowData.fabaoId);

		$("#sixiang_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function effectHandler() {
	let rowData = $("#sixiang_list").datagrid("getSelected");

	if (rowData) {
		$("#effect_list").datagrid("reload", {
			sixiangId: rowData.id
		});

		$("#effect_dialog").dialog("open");
	} else {
		unselected();
	}
}

$(document).ready(function() {

	$("#fabao_id").combobox({
		url: "/fabao_category/selectAll",
		valueField: "id",
		textField: "name",
		panelHeight: "auto",
		prompt: "-法宝类型-",
		groupField: "type",
		groupFormatter: function (group) {
			ajaxGet("/fabao_type/selectById", {
				id: group
			}, function (resp) {
				let data = resp.data;

				$("#fabao_type" + group).html(data.name);
			}, error);

			return "<span class='blue' id='fabao_type" + group + "'></span>";
		},
		formatter: function(row) {
			return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let fabaoId = $("#fabao_id").combobox("getValue");

		$("#sixiang_list").datagrid("load", {
			fabaoId: fabaoId
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#fabaoId").combobox({
		url: "/fabao_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto",
		groupField: "type",
		groupFormatter: function (group) {
			ajaxGet("/fabao_type/selectById", {
				id: group
			}, function (resp) {
				let data = resp.data;

				$("#fabao_type_" + group).html(data.name);
			}, error);

			return "<span class='blue' id='fabao_type_" + group + "'></span>";
		},
		formatter: function(row) {
			return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#type").combobox({
		url: "/sixiang_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto"
	});

	$("#effect").textbox({
		width: 314,
		height: 80,
		required: true,
		multiline: true
	});

	$("#sixiang_dialog").dialog({
		title: "法宝四象",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#sixiang_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#sixiang_dialog").dialog("close");
						$("#sixiang_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#sixiang_form").form("clear");
				$("#sixiang_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	$("#effect_list").datagrid({
		url: "/fabao_sixiang_attribute_effect/selectByPage",
		method: "get",
		title: "四象属性效果列表",
		width: 500,
		height: 300,
		pagination: true,
		pageSize: 5,
		pageList: [5, 10, 20, 50],
		onAfterEdit: function (rowIndex, rowData, changes) { // 结束行内编辑事件
			data = {
				id: rowData.id,
				sixiangId: rowData.sixiangId,
				propertyId: changes.propertyId ? changes.propertyId : rowData.propertyId,
				propertyValue: changes.propertyValue ? changes.propertyValue : rowData.propertyValue
			};
		},
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				let row = $("#sixiang_list").datagrid("getSelected");

				if(row) {
					let datagrid = $("#effect_list");

					datagrid.datagrid("appendRow",{
						id: null,
						sixiangId: row.id,
						propertyId: null,
						propertyValue: 0
					});

					let total = datagrid.datagrid("getRows").length;

					editingId = total - 1;
					requestUrl = "/fabao_sixiang_attribute_effect/insert";

					datagrid.datagrid("beginEdit", editingId);
				} else {
					unselected("请选择一件装备~");
				}
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				let datagrid = $("#effect_list");

				if (editingId != null) {
					datagrid.datagrid("selectRow", editingId);
				} else {
					let row = datagrid.datagrid("getSelected");

					if (row) {
						// 获取行索引，这个索引从0开始
						let rowIndex = datagrid.datagrid("getRowIndex", row);

						editingId = rowIndex;
						requestUrl = "/fabao_sixiang_attribute_effect/updateById";

						datagrid.datagrid("beginEdit", rowIndex);
					} else {
						unselected();
					}
				}
			}
		}, "-", {
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				if (editingId != null) {
					// 只有结束编辑才能获取到最新的值
					$("#effect_list").datagrid("endEdit", editingId);

					ajaxPost(requestUrl, data, function(resp) {
						editingId = null;

						showMsg(resp.message);

						$("#effect_list").datagrid("reload");
					}, error);
				}
			}
		}, "-", {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				if (editingId != null) {
					$("#effect_list").datagrid("cancelEdit", editingId);

					editingId = null;
				}
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				let rowData = $("#effect_list").datagrid("getSelected");

				if(rowData) {
					$.messager.confirm("提示", "是否确认删除？", function(bool) {
						if (bool) {
							ajaxPost("/fabao_sixiang_attribute_effect/deleteById", {
								id: rowData.id
							}, function(resp) {
								showMsg(resp.message);

								$("#effect_list").datagrid("reload");
							}, error);
						}
					});
				} else {
					unselected();
				}
			}
		}],
		columns: [[
			{field: "id", hidden: true},
			{field: "sixiangId", hidden: true},
			{field: "propertyId", title: "属性", align: "center", width: 150
				, editor: {
					type: "combobox",
					options: {
						url: "/property/selectByType?type=3",
						valueField: "id",
						textField: "name",
						panelHeight: "auto"
					}
				}
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/property/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#property_" + rowIndex).html(data.name);
						}, error);

						return "<div class='blue' id='property_" + rowIndex + "'></div>";
					}
				}
			},
			{field: "propertyValue", title: "属性值", align: "center", width: 150
				, editor: {
					type: "numberspinner",
					options: {
						min: 0
					}
				}
			}
		]]
	});

	$("#effect_dialog").dialog({
		title: " ",
		height: 340
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#sixiang_list").datagrid({
		url: "/fabao_sixiang_attribute_category/selectByPage",
		height: height_table,
		method: "get",
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler()
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-shield",
			text: "效果",
			handler: function() {
				effectHandler();
			}
		}],
		columns: [[
			{field: "type", title: "四象类型", align: "center", width: 100,
				formatter: function(value, rowData, rowIndex) {
					ajaxGet("/sixiang_type/selectById", {
						id: value
					}, function (resp) {
						let data = resp.data;

						$("#type_" + rowIndex).html(data.name);
					});

					return "<div id='type_" + rowIndex + "'></div>";
				}
			},
			{field: "effect", title: "四象效果", align: "center", width: 400,
				formatter: function(value) {
					return "<div class='ell'>" + value + "</div>";
				}
			},
			{field: "fabaoId", title: "法宝类型", align: "center", width: 100,
				formatter: function(value, rowData, rowIndex) {
					ajaxGet("/fabao_category/selectById", {
						id: value
					}, function (resp) {
						let data = resp.data;

						$("#fabao_" + rowIndex).attr({
							"alt": data.name,
							"src": data.image,
							"title": data.name
						});
					});

					return "<img height='" + size + "' id='fabao_" + rowIndex + "' />";
				}
			}
		]]
	});

});